

#include<stdio.h>
#include<stdlib.h>



struct node 
	{
	  int element;          /* element stored in the node  */
	  struct node *next;  /* pointer to the next node */
	 };


struct node *mainlist;


void showlist(struct node *list)
	{
		struct node *mypointer ;
		
		mypointer=list;
		if(list==NULL)
			printf("\n\n		The list is EMPTY \n\n");
	
		else
		{		
			printf("\n\n		The list is --->  [ ");		
		
			while(mypointer->next!=NULL)
				{	printf("%d ," ,mypointer->element);
					mypointer=mypointer->next;
				}
			printf("%d ] \n\n\n" ,mypointer->element);
 
			
		}

	}

void createlist(struct node **list)
	{	
	  	struct node *myelement , *temp;
		int element;
		myelement=malloc(sizeof(struct node));
		int yesno  , count=1;
	

		printf(" \n\n		Enter your element number %d : " , count);
		scanf("%d" , &element);
		count++;
		myelement->element=element;
		myelement->next=NULL;								

	

		*list=myelement;
		
		showlist(*list);			


		printf("\n		Do you want to enter more elements in your list (1--> yes , 2--> no ) : ");
		scanf("%d" , &yesno);
		if(yesno==1)
			{	
				myelement=*list;
				do
				{	printf(" \n		Enter your element number %d : " , count);
					scanf("%d" , &element);
					count++;
					while( myelement->next !=NULL)
						{
							myelement=myelement->next;
						}
					temp=malloc(sizeof(struct node));
					temp->element=element;
					temp->next=NULL;
					myelement->next=temp;
					showlist(*list);
					printf("\n		Do you want to enter more elements in your list (1--> yes , 2--> no ) : ");
					scanf("%d" , &yesno);
		
				}while(yesno==1);	
			
			}
		
	}



void append(struct node **list)
	{
		showlist(mainlist);	
		struct node *myelement ;
		struct node *temp;
		int element;
		myelement=malloc(sizeof(struct node));
		printf(" \n		Enter the element that you wish to append in list : " );
		scanf("%d" , &element);
		myelement=*list;
		while( myelement->next !=NULL)
			{
				myelement=myelement->next;
			}
		temp=malloc(sizeof(struct node));
		temp->element=element;
		temp->next=NULL;
		myelement->next=temp;
		printf("\n\n		SUCCESS !!  Your element %d has been added to the list . \n\n" , element );
		showlist(mainlist);
				
	}


void extend(struct node **list)
	{
		showlist(mainlist);	
		printf(" \n		Enter the new list that you want to extend ----> \n");
		struct node *templist;
		createlist(&templist);			
		struct node *myelement ;
		myelement=*list;
		while( myelement->next !=NULL)
			{
				myelement=myelement->next;
			}
		myelement->next=templist;
		printf("\n\n		SUCCESS !! The list has been extended !! \n\n");
		showlist(mainlist);
	}		



void insert(struct node **list)
	{
		showlist(mainlist);
		int position , element , count=0,flag=0;
		printf(" \n		Enter the position at which you want to insert the new element : ");
		scanf("%d" , &position);
		position=position-1;
		struct node *myelement , *temp;
		myelement=*list;
		while( myelement->next !=NULL)
			{	if(position==0)
					{	flag=1;
						printf(" \n		Enter the element that you wish to insert : ");
						scanf("%d" , &element);
						temp=malloc(sizeof(struct node));
						temp->element=element;
						temp->next=myelement;
						*list=temp;
						break;
					}
				
				if(position-1==count)
					{
						flag=1;
						printf(" \n		Enter the element that you wish to insert : ");
						scanf("%d" , &element);
						temp=malloc(sizeof(struct node));
						temp->element=element;
						temp->next=myelement->next;
						myelement->next=temp;
						break;	
					}
				myelement=myelement->next;
				count++;	

			}
	
		if(flag==1)
			printf("\n\n		SUCCESS !!  Your element %d has been inserted at position %d in the list . \n\n" , element , (position+1) );
		else
			printf("\n\n		FAILED !!  Position %d doesnt exist in the list . \n\n" , (position+1) );
					
		showlist(mainlist);

	}



void removal(struct node **list)
	{
		showlist(mainlist);
		int element , flag=0;
		printf(" \n		Enter the element you wish to remove the first occurence of from your list : ");
		scanf("%d" , &element);
		struct node *myelement , *temp;
		myelement=*list;

		
		if((myelement!=NULL)&&(myelement->next==NULL))
			if((myelement->element)==element)
				{
					flag=1;
					*list=NULL;
				}

		while( myelement->next !=NULL)
			{	
				if(myelement->element==element)
					{
						flag=1;
						*list=myelement->next;
					 	break;
					}
				
	
				temp=myelement->next;
				if(temp->element==element)
					{
						flag=1;
						myelement->next=temp->next;
						break;	
					}
				myelement=myelement->next;
				

			}
	
		if(flag==1)
			printf("\n\n		SUCCESS !!  Your element %d has been removed from its first occurence in the list . \n\n" , element );
		else
			printf("\n\n     	FAILED !!  Element %d doesnt exist in the list . \n\n" , element );
					
		showlist(mainlist);

	}



int pop(struct node **list)
	{
		showlist(mainlist);
		int position , popreturn=-1 , count=0,flag=0;
		printf(" \n   		Enter the position at which you want to pop the element : ");
		scanf("%d" , &position);
		position=position-1;
		struct node *myelement , *temp;
		myelement=*list;

		if((position==0)&&((myelement!=NULL)&&(myelement->next==NULL)))
			{	flag=1;
				popreturn=myelement->element;
				*list=NULL;

			}


		while( myelement->next !=NULL)
			{	if(position==0)
					{	flag=1;
						popreturn=myelement->element;
						*list=myelement->next;
						break;
					}
				
				temp=myelement->next;
				if(position-1==count)
					{
						flag=1;
						popreturn=temp->element;
						myelement->next=temp->next;
						break;	
					}
				myelement=myelement->next;
				count++;	

			}
				
			
				
		

		
	
		if(flag==1)
			printf("\n\n  		SUCCESS !!  Element %d has been popped from position %d in the list . \n\n" , popreturn , (position+1) );
		else
			printf("\n\n  		FAILED !!  Position %d doesnt exist in the list . \n\n" , (position+1) );
					
		showlist(mainlist);
		return popreturn;

	}		


int indexs(struct node **list)
	{
		showlist(mainlist);
		int element , flag=0,count=1, indexreturn=-1;
		printf(" \n		Enter the element you wish to find the first occurence of in your list : ");
		scanf("%d" , &element);
		struct node *myelement , *temp;
		myelement=*list;
		while( myelement->next !=NULL)
			{	
				if(myelement->element==element)
					{	indexreturn=count;
						flag=1;
					 	break;
					}
				myelement=myelement->next;
				count++;
				

			}

		if(flag==0)
			if(myelement->element==element)
				{
					flag=1;
					indexreturn=count;
				}

		if(flag==1)
			printf("\n\n		SUCCESS !!  Element %d is found to have its first occurence at position %d in the list . \n\n" , element , indexreturn);
		else
			printf("\n\n     	FAILED !!  Element %d doesnt exist in the list . \n\n" , element );
					
		showlist(mainlist);
		return indexreturn;
		
	}



int count(struct node **list)
	{
		showlist(mainlist);
		int element , countreturn=0;
		printf(" \n		Enter the element you wish to find the count of in your list : ");
		scanf("%d" , &element);
		struct node *myelement ;
		myelement=*list;
		while( myelement->next !=NULL)
			{	
				if(myelement->element==element)
					{	
						countreturn++;						
					}
				myelement=myelement->next;
				
			}

		if(myelement->element==element)
			{
				countreturn++;	
			}

		
		printf("\n\n		Element %d occurs %d times in the list . \n\n" , element , countreturn);
		
		if(countreturn==0)
			countreturn=-1;		
					
		showlist(mainlist);
		return countreturn;
		
	}



void sort(struct node **list)
	{
		struct node *myelement , *small , *temp ;
		int swap;
		myelement=*list;
		showlist(mainlist);
		while(myelement->next!=NULL)
			{
				small=myelement	;
				temp=myelement->next;
				while(temp->next!=NULL)
					{
						if((temp->element)<(small->element))
							{
								small=temp;
							}
						temp=temp->next;

					}

				if((temp->element)<(small->element))
							{
								small=temp;
							}			
				
				if((small->element)!=(myelement->element))
					{
						swap=small->element;
						small->element=myelement->element;
						myelement->element=swap;
					}
				
				myelement=myelement->next;
			}

		printf("\n\n		SUCCESS !!  The list has been sorted. \n\n" );
		showlist(mainlist);						
	}



void reverse(struct node **list)
{
	showlist(mainlist);	
	struct node *myelement , *temp=NULL , *temp1=NULL;

	myelement=*list;	
	
	while(myelement!=NULL)
		{
		 *list=myelement;
		  temp1=myelement->next;
		  myelement->next=temp;
		  temp=myelement;
		  myelement=temp1;
		}			
			
	printf("\n\n		SUCCESS !!!   The list has been reversed !!! \n\n");
	showlist(mainlist);	

}

int listsize(struct node **list)
	{
		int elementcount=0;
		showlist(mainlist);
		struct node *counter;
		counter=*list;

		if(*list!=NULL)
		{
			while(counter->next!=NULL)
			{
				elementcount++;
				counter=counter->next;
			}
			if(counter!=NULL)
				elementcount++;
		}
			
		printf("\n\n		The number of elements in your list is : %d \n\n " , elementcount );		

		if(elementcount==0)
			elementcount=-1;
		return elementcount;
	}



void main()
	{

		int choice=0,flag=0 , indexreturn=-1 , countreturn=-1 , elementcount=-1 , popreturn=-1;
		while (choice!=13)
	           {
			if(flag==0)
				{
				  flag=1;
			  	  createlist(&mainlist);				
				}
	
			printf("\n\n\n    	         MAIN MENU !!!     \n");
			printf("\n	 What do you want to do ??? \n");
			printf("\n	 1.  CREATE  --- Discard your present list and make a new list.");
			printf("\n	 2.  APPEND  --- Append  an item at the end of your list.");
			printf("\n	 3.  EXTEND  --- Extend the list by appending all the items in a list at the end.");
			printf("\n	 4.  INSERT  --- Add an element in the list at a particular position.");
			printf("\n	 5.  REMOVE  --- Remove the first occurence of an element from your list.");
			printf("\n 	 6.  POP     --- Pop the item at a given position in your list and return it.");
			printf("\n	 7.  INDEX   --- Return the index of the first occurence of an item in your list.");
			printf("\n	 8.  COUNT   --- Return the number of times an element appears in the list");
			printf("\n	 9.  SORT    --- Sort the elements of the list.");
			printf("\n	 10. REVERSE --- Reverse the elements of your list.");
			printf("\n	 11. SHOW    --- Displays the list.");
			printf("\n	 12. SIZE    --- Returns the total number of elements in the list.");
			printf("\n	 13. EXIT    --- Exit the program.");
			printf("\n\n      Enter the function number of your choice : ");
			scanf("%d" , &choice);

		 	int checkswitch=0;
			if((mainlist==NULL)&&((choice!=13)&&(choice!=1)))
				{
										
					printf("\n\n		The list is empty , create a new list to continue\n");
					checkswitch=1;

				}					
			
			while(checkswitch==0)
			{
			   switch(choice)
				{
					case 1 : createlist(&mainlist);	
						 checkswitch=1;
						 break;
					
					case 2 : append(&mainlist);	
						 checkswitch=1;
						 break;

					case 3 : extend(&mainlist);
						 checkswitch=1;
						 break;

					case 4 : insert(&mainlist);
						 checkswitch=1;
						 break;

					case 5 : removal(&mainlist);
						 checkswitch=1;
						 break;

					case 6 : popreturn=pop(&mainlist);
						 checkswitch=1;
						 break;

					case 7 : indexreturn=indexs(&mainlist);
						 checkswitch=1;
						 break;

					case 8 : countreturn=count(&mainlist);
						 checkswitch=1;
						 break;

					case 9 : sort(&mainlist);
						 checkswitch=1;
						 break;

					case 10 :reverse(&mainlist);
						 checkswitch=1;
						 break;

					case 11 : showlist(mainlist);
						  checkswitch=1;
						  break;
					case 12 : elementcount=listsize(&mainlist);
						  checkswitch=1;
						  break;


					case 13 : printf("\n\n Thank you for using this program.   \n\n ");
						  checkswitch=1;
						  break;

					default :
						  printf("\n\n The choice you entered wasnt valid !! \n Please choose again !!\n");
						  choice=0;
					
						  while((choice<1)||(choice>10))
							{
					 			printf("\n\n     Enter the function number of your choice : ");
								scanf("%d" , &choice);
							}
				
				}
		
			}

		}

}
	
	
		
